﻿# CMakeList.txt: 顶层 CMake 项目文件，在此处执行全局配置
# 并包含子项目。
#
cmake_minimum_required (VERSION 3.8)

if(WIN32)
	message("vcpkg source path:${VCPKG_ROOT}")
	set(VCPKG_ROOT "J:/openLib/vcpkg" CACHE PATH "") 
	message("vcpkg path:${VCPKG_ROOT}")
	# 启用 vcpkg 清单模式
	set(VCPKG_MANIFEST_MODE OFF)

	set(CMAKE_TOOLCHAIN_FILE "J:/openLib/vcpkg/scripts/buildsystems/vcpkg.cmake")
	set(CMAKE_PREFIX_PATH "J:/openLib/vcpkg/scripts/buildsystems")
	message("--------prefix path:${CMAKE_PREFIX_PATH} cmake:${CMAKE_SOURCE_DIR}")

	# 设置自定义编译器
	set(CMAKE_C_COMPILER "${CMAKE_SOURCE_DIR}/incredibuild_wrapper.bat")
	set(CMAKE_CXX_COMPILER "${CMAKE_SOURCE_DIR}/incredibuild_wrapper.bat")
ENDIF()


project(as_lib)
set(CMAKE_CXX_STANDARD 20)
set(PROJECT_BIN_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin)

#头文件路径
set(HEADERS_PATH
    ""
	"${CMAKE_CURRENT_SOURCE_DIR}/tools"
)

#项目路径
set(TEST_SRC "${CMAKE_CURRENT_SOURCE_DIR}/test")

#第三方库
set(BASE_THRID_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/3rd")
include_directories(${BASE_THRID_ROOT})

#设置库源码位置
IF(CMAKE_SYSTEM_NAME MATCHES "Linux")
	set(BOOST_ROOT "/usr/local/include/boost")
	set(WEPOLL_SRC_LIST "")
	add_compile_options(-fpermissive)
	message("linux matches")
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Windows")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus")
	set(BOOST_ROOT "J:/openLib/boost_1_70_0")
	set(ASYNC_BASE "J:/openLib/async_simple")
	set(ASYNC_INCLUDE "${ASYNC_BASE}/include")
	set(ASYNC_LIB "${ASYNC_BASE}/lib")
	message("windows matches")
ENDIF()

if(WIN32)
#添加宏定义
add_definitions(-DWIN32_LEAN_AND_MEAN)		
add_definitions(-D_WIN32_WINDOWS)			#设置windows宏
add_definitions(-DWIN32)
#add_definitions(-DUSE_BIG_ENDIAN)			#定义是否大端
add_definitions(-D_WINSOCK_DEPRECATED_NO_WARNINGS)
ELSE()
add_definitions(-D_DEBUG)
ENDIF()

option(OPENVDB_INSTALL_CMAKE_MODULES "install cmake modules" ON)

if(OPENVDB_INSTALL_CMAKE_MODULES)
  #set(OPENVDB_CMAKE_MODULES
  #  cmake/FindJemalloc.cmake
  #)
  #install(FILES ${OPENVDB_CMAKE_MODULES} DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/lib)
  set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
endif()


#设置库位置
set(SERVER_LIB "${CMAKE_CURRENT_SOURCE_DIR}/lib")

##公共代码目录
set(COMMON_SRC "${CMAKE_CURRENT_SOURCE_DIR}/src/common")

##服务器代码
set(SERVER_SRC "${CMAKE_CURRENT_SOURCE_DIR}/src")

##第三方目录
set(THIRD_LIB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/3rd")

if(WIN32)
	set(TARGET_OPTIONS "/source-charset:utf-8" "/execution-charset:utf-8" "/EHsc")
	set(GLOBAL_LIB "DbgHelp" "as_tools")
else()
	set(TARGET_OPTIONS "")
	set(GLOBAL_LIB "as_tools")
endif()

cmake_policy(PUSH)
#设置忽略警告
IF(CMAKE_SYSTEM_NAME MATCHES "Windows")
add_compile_options(/wd4819 /wd4244 /wd4251 /wd4996 /wd4828 /wd4005)
ENDIF()
cmake_policy(POP)

######################使用多处理器编译########################
IF (WIN32)
        if(MSVC)
                OPTION(USE_MP "use multiple" ON)
                OPTION(ProjectConfig_Global_COMPILE_FLAGS_WITH_MP
                    "Set The Global Option COMPILE_FLAGS /MP to target." ON)
                if(ProjectConfig_Global_COMPILE_FLAGS_WITH_MP OR USE_MP)
                    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP")
                    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
                endif()
                set(VS_STARTUP_PROJECT ${PROJECT_NAME})
        ENDIF(MSVC)
ENDIF()

set(TOLUA_BUILD_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tools/tolua")

file(GLOB TOOLS_SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/tools/*.cpp")
file(GLOB TOOLS_INC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/tools/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/tools/*.inl")
file(GLOB NET_SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/net/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/net/*.cpp")
file(GLOB SNET_SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/snet/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/snet/*.cpp")
file(GLOB INET_SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/inet/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/inet/*.cpp")
file(GLOB ANET_SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/anet/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/anet/*.cpp")
file(GLOB ACTOR_SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/actor/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/actor/*.cpp")
file(GLOB GAME_SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/game/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/game/*.cpp")

#全局对象
set(THIRD_FILE_LIST "")
set(AS_LINK_LIB "")

option(IS_USE_GLOG "is use glog" OFF)
if(IS_USE_GLOG)
	add_definitions(-DUSE_GLOG)
endif()

add_definitions(-DGLOG_NO_ABBREVIATED_SEVERITIES)

option(IS_USE_JEM "is use jem" OFF)
if(IS_USE_JEM)
	add_definitions(-DUSE_JEMALLOC)
endif()

option(USE_STACK_WALKER "is use stack walker" OFF)
IF(USE_STACK_WALKER)
	set(STACK_WALKER_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/3rd/stackwalker")
	file(GLOB STACK_WALKER_FILE "${STACK_WALKER_ROOT}/*")
	LIST(APPEND THIRD_FILE_LIST ${STACK_WALKER_FILE})

	add_definitions(-D _CRT_SECURE_NO_WARNINGS)
	add_definitions(-D AS_STACK_WALKER)
	message("use stack_walker tools path:${STACK_WALKER_ROOT}")
	include_directories("${STACK_WALKER_ROOT}")
ENDIF()

option(USE_REDIS "is use redis" OFF)
if(USE_REDIS)
	#redis
	find_package(hiredis REQUIRED)
	add_definitions(-DAS_REDIS)
	file(GLOB REDIS_SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/tools/redis/*.cpp")
	file(GLOB REDIS_INC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/tools/redis/*.h")
	LIST(APPEND TOOLS_SRC_LIST ${REDIS_SRC_LIST})
	LIST(APPEND TOOLS_INC_LIST ${REDIS_INC_LIST})
	LIST(APPEND AS_LINK_LIB hiredis::hiredis)
	IF(CMAKE_SYSTEM_NAME MATCHES "Windows")
		add_compile_options(/wd4200)
		source_group("redis" FILES ${REDIS_INC_LIST} ${REDIS_SRC_LIST})
	ENDIF()
endif()

option(USE_RAPIDJSON "is use rapidjson" OFF)
if(USE_RAPIDJSON)
	#json
	set(RAPIDJSON_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/3rd/rapidjson")
	file(GLOB RAPIDJSON_INC_LIST ${RAPIDJSON_ROOT}/*.h)
	LIST(APPEND THIRD_FILE_LIST ${RAPIDJSON_INC_LIST})
ENDIF()

option(USE_MYSQL "is use mysql" OFF)
if(USE_MYSQL)
	add_definitions(-DAS_MYSQL)
	set(MYSQL_INCLUDE_PATH "J:/openLib/mysql8.2")

	LIST(APPEND HEADERS_PATH ${MYSQL_INCLUDE_PATH}/include)
	LIST(APPEND AS_LINK_LIB "${MYSQL_INCLUDE_PATH}/lib/libmysql.lib")

	file(GLOB MYSQL_SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/tools/mysql/*.cpp")
	file(GLOB MYSQL_INC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/tools/mysql/*.h")
	LIST(APPEND TOOLS_SRC_LIST ${MYSQL_SRC_LIST})
	LIST(APPEND TOOLS_INC_LIST ${MYSQL_INC_LIST})
	IF(CMAKE_SYSTEM_NAME MATCHES "Windows")
		source_group("mysql" FILES ${MYSQL_INC_LIST} ${MYSQL_SRC_LIST})
	ENDIF()
ENDIF()

option(USE_LUA "is use lua" ON)
if(USE_LUA)
	add_definitions(-DAS_LUA)
	set(LUA_PATH ${THIRD_LIB_PATH}/lua)
	set(TOLUA_PATH ${THIRD_LIB_PATH}/tolua)
	file(GLOB LUA_INC ${LUA_PATH}/*.h)
	file(GLOB TOLUA_INC ${TOLUA_PATH}/*.h)
	LIST(APPEND HEADERS_PATH ${LUA_PATH} ${TOLUA_PATH})
	LIST(APPEND THIRD_FILE_LIST ${LUA_INC} ${TOLUA_INC})
	LIST(APPEND AS_LINK_LIB lua tolua)
	IF(CMAKE_SYSTEM_NAME MATCHES "Windows")
		source_group("3rd\\lua" FILES ${LUA_INC})
	ENDIF()
ENDIF()

set(LUA_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/lib")

include_directories(${HEADERS_PATH})

###生成工具库lib
source_group("3rd" FILES ${THIRD_FILE_LIST})
source_group("inc" FILES ${TOOLS_INC_LIST})
source_group("src" FILES ${TOOLS_SRC_LIST})
add_library(as_tools ${TOOLS_INC_LIST} ${TOOLS_SRC_LIST} ${THIRD_FILE_LIST})
target_compile_options(as_tools PRIVATE ${TARGET_OPTIONS})
target_link_libraries(as_tools PRIVATE ${AS_LINK_LIB})
set_target_properties(as_tools PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY_DEBUG  "${SERVER_LIB}"
    LIBRARY_OUTPUT_DIRECTORY_DEBUG "${SERVER_LIB}"
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${SERVER_LIB}"
    ARCHIVE_OUTPUT_DIRECTORY_RELEASE  "${SERVER_LIB}"
    LIBRARY_OUTPUT_DIRECTORY_RELEASE "${SERVER_LIB}"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${SERVER_LIB}"
)

link_directories(${SERVER_LIB})
#message("CMAKE_MODULE_PATH path:${CMAKE_MODULE_PATH} bin:${CMAKE_BINARY_DIR}")


set(testdeplibs)
list(APPEND testdeplibs ${CMAKE_THREAD_LIBS_INIT})

#生成exe函数
function(build_project)
	cmake_parse_arguments(AS_BUILD_EXE
	"PUBLIC"
	"NAME"
	"SRCS;INC_DIR;LIB_DIR;LINK_LIB"
	${ARGN}
	)

	message("build src:${AS_BUILD_EXE_SRCS}")
	add_executable(${AS_BUILD_EXE_NAME} ${AS_BUILD_EXE_SRCS})

	target_compile_options(${AS_BUILD_EXE_NAME} PRIVATE ${TARGET_OPTIONS})

	target_include_directories(${AS_BUILD_EXE_NAME} PRIVATE ${AS_BUILD_EXE_INC_DIR})

	target_link_directories(${AS_BUILD_EXE_NAME} PRIVATE ${AS_BUILD_EXE_LIB_DIR})

	target_link_libraries(${AS_BUILD_EXE_NAME} PRIVATE
		${GLOBAL_LIB}
		${AS_BUILD_EXE_LINK_LIB}
	)

	set_target_properties(${AS_BUILD_EXE_NAME} PROPERTIES 
        VS_DEBUGGER_WORKING_DIRECTORY ${PROJECT_BIN_PATH}
        RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BIN_PATH}
    )
endfunction()



add_subdirectory(${TEST_SRC})


